import request from 'utils/request'
import { apiPrefix } from 'utils/config'

import api from './api'

const gen = (params) => {
  let url = apiPrefix + params
  let method = 'GET'
  const paramsArray = params.split(' ')
  if (paramsArray.length === 2) {
    method = paramsArray[0]
    url = apiPrefix + paramsArray[1]
  }

  return function (data) {
    return request({
      url,
      data,
      method,
    })
  }
}

const APIFunction = {
  results: [
    {
      location: {
        id: 'WS10730EM8EV',
        name: '深圳',
        country: 'CN',
        path: '深圳,深圳,广东,中国',
        timezone: 'Asia/Shanghai',
        timezone_offset: '+08:00',
      },
      now: {
        text: '多云',
        code: '4',
        temperature: '18',
      },
      last_update: '2021-03-04T13:24:30+08:00',
    },
  ],
}
for (const key in api) {
  APIFunction[key] = gen(api[key])
}

APIFunction.queryWeather = (params) => {
  params.key = 'i7sau1babuzwhycn'
  return request({
    url: `${apiPrefix}/weather/now.json`,
    // url: `http://192.168.12.151:8000/api/v1/weather/now.json`,
    data: params,
  })
}
export default APIFunction
